package com.kgc.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.kgc.pojo.Manager;
import com.kgc.service.GetMangerInfo_wzlService;
import com.kgc.util.Constants;
import com.kgc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

/**
 * 获得管理员的信息
 * @author  王臻利
 */
@RestController
public class GetManagerInfo_wzlController {
    @Autowired
    private GetMangerInfo_wzlService getMangerInfo_wzlService;

    @PostMapping("getManagerInfo_wzl")
    public Object getManagerInfo(@RequestParam String username, @RequestParam String password,
                                 HttpServletResponse response) throws UnsupportedEncodingException {
        Manager managers =getMangerInfo_wzlService.getManagerInfo(username,password);
        if(managers!=null) {
            String jsonString= JSONObject.toJSONString(managers);
            /*进行编码*/
            jsonString = URLEncoder.encode(jsonString,"Utf-8");
            /*存入cookies中*/
            Cookie cookie = new Cookie(Constants.LOGIN_KEY,jsonString);
            /*设置cookie路径*/
            cookie.setPath("/");
            /*设置时间半小时*/
            cookie.setMaxAge(60*30);
            /*存入浏览器中*/
            response.addCookie(cookie);

            return new Result(true,"恭喜您登录成功");
        }
        return new Result(false,"sorry登录失败");
    }

    /*获取管理员登录的名字*/
    @GetMapping("getMangername_wzl")
    public Object getMangername_wzl(HttpServletRequest request) throws UnsupportedEncodingException {
        Cookie[] cookies = request.getCookies();
        String manager="";
        if(cookies!=null&&cookies.length>0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(Constants.LOGIN_KEY)) {
                    manager = cookie.getValue();
                    /*解码*/
                    manager = URLDecoder.decode(manager, "utf-8");
                }
            }
        }
            return  new Result(manager,"获取登录用户名成功");
    }
    /*管理员cookie注销*/
    @GetMapping("logout_wzl")
    public Object logout_wzl(HttpServletResponse response){
        //覆盖用户登录的Cookie
        Cookie cookie = new Cookie(Constants.LOGIN_KEY, "");
        cookie.setPath("/");
        cookie.setMaxAge(60*30);//超时时间，1小时
        //存储Cookie
        response.addCookie(cookie);

        return new Result(true, "注销成功");
    }
    /*获取管理员全部信息*/
    @GetMapping("getManagerinfo_wzl")
    public Object getManagerInfo_wzl(){
        List<Manager> list = getMangerInfo_wzlService.getManagerInfo_wzl();
        return new Result(list,"获取管理员信息成功");
    }
    /*删除管理员信息*/
    @PostMapping("deleManagerInfo_wzl")
    public Object deleManagerInfo_wzl(int id){
        int count =getMangerInfo_wzlService.deleManager_wzl(id);
        return new Result(count,"删除用户成功");
    }
    /*增加管理员信息*/
    @PostMapping("addMananger_wzl")
    public Object addMananger_wzl(Manager manager){
        int count =getMangerInfo_wzlService.addManagerinfo_wzl(manager);
        return  new Result(count,"增加管理员用户成功");
    }
}
